﻿using System;
using System.Collections.Generic;
//
using PerceptiveMCAPI;
using PerceptiveMCAPI.Types;
//

namespace PerceptiveMCAPI_Test
{
   public partial class get_campaignUpdate : System.Web.UI.UserControl
   {
      public ApiMethod method { get; set; }

      public string campaignId
      {
         get { return tbCampaignId.Text.Trim(); }
         set { tbCampaignId.Text = value; }
      }
      public string name
      {
         get { return hidName.Value; }
      }
      public object value
      {
         get { return GetValueFromPageEntry(); }
      }
      // ------------------------------------------------------------
      public void Setup()
      {
         this.Visible = true;
         lblSection.Text = method.section;
         lblMethod.Text = method.name;
         ddlName.SelectedIndex = 0;
         ResetPage();
      }

      protected void ddlName_SelectedIndexChanged( object sender, EventArgs e )
      {
         ResetPage();
         //
         switch ( ddlName.SelectedValue )
         {
            case "list_id":
            case "subject":
            case "from_email":
            case "from_name":
            case "to_email":
            case "template_id":
            case "folder_id":
            case "title":
               hidName.Value = ddlName.SelectedValue;
               divStringValue.Visible = true;
               tbValue.Focus();
               break;
            case "authenticate":
            case "auto_footer":
            case "inline_css":
            case "generate_text":
            case "auto_tweet":
            case "ecomm360":
            case "timewarp":
               hidName.Value = ddlName.SelectedValue;
               divBooleanValue.Visible = true;
               cbTrueOrFalse.Focus();
               //
               if ( ddlName.SelectedValue == "authenticate" )
                  cbTrueOrFalse.Checked = MCAPISettings.default_campaign_authenticate;
               else if ( ddlName.SelectedValue == "inline_css" )
                  cbTrueOrFalse.Checked = MCAPISettings.default_campaign_inline_css;
               else if ( ddlName.SelectedValue == "generate_text" )
                  cbTrueOrFalse.Checked = MCAPISettings.default_campaign_generate_text;
               else if ( ddlName.SelectedValue == "auto_tweet" )
                  cbTrueOrFalse.Checked = MCAPISettings.default_campaign_auto_tweet;
               else if ( ddlName.SelectedValue == "timewarp" )
                  cbTrueOrFalse.Checked = MCAPISettings.default_campaign_timewarp;
               else if ( ddlName.SelectedValue == "ecomm360" )
                  cbTrueOrFalse.Checked = MCAPISettings.default_campaign_ecomm360;
               else if ( ddlName.SelectedValue == "auto_footer" )
                  cbTrueOrFalse.Checked = MCAPISettings.default_campaign_auto_footer;
               break;
            case "tracking":
               hidName.Value = ddlName.SelectedValue;
               divTracking.Visible = true;
               cbOpens.Focus();
               //
               cbOpens.Checked = MCAPISettings.default_campaign_tracking_opens;
               cbHtmlClicks.Checked = MCAPISettings.default_campaign_tracking_html_clicks;
               cbTextClicks.Checked = MCAPISettings.default_campaign_tracking_text_clicks;
               break;
            case "analytics":
               hidName.Value = ddlName.SelectedValue;
               divAnalytics.Visible = true;
               tbSvcType.Focus();
               break;
            case "content":
               hidName.Value = "content";
               divContent.Visible = true;
               divContentHtml.Visible = true;
               divContentText.Visible = true;
               tbContentHtml.Focus();
               break;
            case "content_url":
               hidName.Value = "content";
               divContent.Visible = true;
               divContentUrl.Visible = true;
               divContentText.Visible = true;
               tbContentUrl.Focus();
               break;
            case "content_template":
               hidName.Value = "content";
               divContent.Visible = true;
               divContentTemplate.Visible = true;
               divContentText.Visible = true;
               break;
            case "content_archive":
               hidName.Value = "content";
               divContent.Visible = true;
               divContentArchive.Visible = true;
               upload_archive.Focus();
               break;
            case "segment_opts":
               hidName.Value = "segment_opts";
               divSegmentOptions.Visible = true;
               sub_SegmentOptions1.Setup();
               break;
            case "type_opts_rss":
               hidName.Value = "rss";
               divTypeOptsRss.Visible = true;
               sub_RssOptions1.Setup();
               break;
            case "type_opts_absplit":
               hidName.Value = "absplit";
               divTypeOptsABSplit.Visible = true;
               tbABSVal01.Focus();
               break;
            case "type_opts_auto":
               hidName.Value = "auto";
               divTypeOptsAutoResponder.Visible = true;
               ddlOffsetUnits.Focus();
               break;
            default:
               break;
         }
      }

      private void ResetPage()
      {
         divStringValue.Visible = false;
         divBooleanValue.Visible = false;
         divContent.Visible = false;
         divContentHtml.Visible = false;
         divContentText.Visible = false;
         divContentUrl.Visible = false;
         divContentTemplate.Visible = false;
         divContentArchive.Visible = false;
         divTracking.Visible = false;
         divAnalytics.Visible = false;
         divSegmentOptions.Visible = false;
         divTypeOptsRss.Visible = false;
         divTypeOptsABSplit.Visible = false;
         divTypeOptsAutoResponder.Visible = false;
         //
         hidName.Value = "";
         tbValue.Text = "";
         cbTrueOrFalse.Checked = false;
         // analytics
         tbSvcTag.Text = "";
         tbSvcType.Text = "";
         // tracking
         cbOpens.Checked = false;
         cbHtmlClicks.Checked = false;
         cbTextClicks.Checked = false;
         // content
         tbContentHtml.Text = "";
         tbContentText.Text = "";
         tbContentUrl.Text = "";
         // template content
         sub_TemplateSections1.TemplateSections = new Dictionary<string, string>();
         // 
         // type_opts, rss
         // type_opts, absplit
         // type_opts, auto
      }
      private object GetValueFromPageEntry()
      {
         object obj = new object();
         //
         switch ( ddlName.SelectedValue )
         {
            case "list_id":
            case "subject":
            case "from_email":
            case "from_name":
            case "to_email":
            case "template_id":
            case "title":
            case "folder_id":
               obj = tbValue.Text.Trim();
               break;
            case "authenticate":
            case "inline_css":
            case "generate_text":
            case "auto_tweet":
            case "auto_footer":
            case "timewarp":
            case "ecomm360":
               obj = cbTrueOrFalse.Checked;
               break;
            case "tracking":
               campaignTracking tracking =
                  new campaignTracking( cbOpens.Checked, cbHtmlClicks.Checked, cbTextClicks.Checked );
               obj = tracking;
               break;
            case "analytics":
               Dictionary<string, string> analytics = new Dictionary<string, string>();
               if ( !string.IsNullOrEmpty( tbSvcType.Text.Trim() ) )
                  analytics.Add( tbSvcType.Text.Trim(), tbSvcTag.Text.Trim() );
               obj = analytics;
               break;
            case "content":
               Dictionary<string, string> contentDictionary = new Dictionary<string, string>();
               if ( !string.IsNullOrEmpty( tbContentHtml.Text.Trim() ) )
                  contentDictionary.Add( "html", tbContentHtml.Text.Trim() );
               if ( !string.IsNullOrEmpty( tbContentText.Text.Trim() ) )
                  contentDictionary.Add( "text", tbContentText.Text.Trim() );
               obj = contentDictionary;
               break;
            case "content_url":
               Dictionary<string, string> contentUrlDictionary = new Dictionary<string, string>();
               if ( !string.IsNullOrEmpty( tbContentUrl.Text.Trim() ) )
                  contentUrlDictionary.Add( "url", tbContentUrl.Text.Trim() );
               if ( !string.IsNullOrEmpty( tbContentText.Text.Trim() ) )
                  contentUrlDictionary.Add( "text", tbContentText.Text.Trim() );
               obj = contentUrlDictionary;
               break;
            case "content_template":
               Dictionary<string, string> contentTemplateDictionary = sub_TemplateSections1.TemplateSections;
               if ( !string.IsNullOrEmpty( tbContentText.Text.Trim() ) )
                  contentTemplateDictionary.Add( "text", tbContentText.Text.Trim() );
               obj = contentTemplateDictionary;
               break;
            case "content_archive":
               Dictionary<string, string> archive = get_Archive_encoded();
               obj = archive;
               break;
            case "segment_opts":
               campaignSegmentOptions opts = new campaignSegmentOptions();
               opts.match = sub_SegmentOptions1.Match;
               opts.conditions = sub_SegmentOptions1.SegmentConditions;
               obj = opts;
               break;
            case "type_opts_rss":
               obj = sub_RssOptions1.type_opts;
               break;
            case "type_opts_absplit":
               Dictionary<string, string> absplit = getABSplitOptions();
               obj = absplit;
               break;
            case "type_opts_auto":
               Dictionary<string, string> autoResp = getAutoResponderOptions();
               obj = autoResp;
               break;
            default:
               break;
         }
         return obj;
      }

      // --------------------------------------------------------------------------------------
      private Dictionary<string, string> getAutoResponderOptions()
      {
         Dictionary<string, string> options = new Dictionary<string, string>();

         options.Add( lblOffsetUnits.Text, ddlOffsetUnits.SelectedValue );
         options.Add( lblOffsetTime.Text, tbOffsetTime.Text.Trim() );
         options.Add( lblOffsetDir.Text, ddlOffsetDir.SelectedValue );
         options.Add( lblEvent.Text, ddlEvent.SelectedValue );
         if ( ddlEvent.SelectedValue == "date"
            || ddlEvent.SelectedValue == "annual" )
         {
            options.Add( lblEventDatemerge.Text.Trim(), tbEventDatemerge.Text.Trim() );
         }
         return options;
      }
      // --------------------------------------------------------------------------------------
      private Dictionary<string, string> getABSplitOptions()
      {
         Dictionary<string, string> options = new Dictionary<string, string>();
         for ( int i = 1; i <= 10; i++ )
         {
            switch ( i )
            {
               case 1:
                  options.Add( lblABSOpt01.Text, tbABSVal01.Text.Trim() );
                  break;
               case 2:
                  options.Add( lblABSOpt02.Text, tbABSVal02.Text.Trim() );
                  break;
               case 3:
                  if ( !string.IsNullOrEmpty( tbABSOpt03.Text.Trim() ) )
                     options.Add( tbABSOpt03.Text.Trim(), tbABSVal03.Text.Trim() );
                  break;
               case 4:
                  if ( !string.IsNullOrEmpty( tbABSOpt04.Text.Trim() ) )
                     options.Add( tbABSOpt04.Text.Trim(), tbABSVal04.Text.Trim() );
                  break;
               case 5:
                  if ( !string.IsNullOrEmpty( tbABSOpt05.Text.Trim() ) )
                     options.Add( tbABSOpt05.Text.Trim(), tbABSVal05.Text.Trim() );
                  break;
               case 6:
                  if ( !string.IsNullOrEmpty( tbABSOpt06.Text.Trim() ) )
                     options.Add( tbABSOpt06.Text.Trim(), tbABSVal06.Text.Trim() );
                  break;
               case 7:
                  if ( !string.IsNullOrEmpty( tbABSOpt07.Text.Trim() ) )
                     options.Add( tbABSOpt07.Text.Trim(), tbABSVal07.Text.Trim() );
                  break;
               case 8:
                  if ( !string.IsNullOrEmpty( tbABSOpt08.Text.Trim() ) )
                     options.Add( tbABSOpt08.Text.Trim(), tbABSVal08.Text.Trim() );
                  break;
               case 9:
                  if ( !string.IsNullOrEmpty( tbABSOpt09.Text.Trim() ) )
                     options.Add( tbABSOpt09.Text.Trim(), tbABSVal09.Text.Trim() );
                  break;
               case 10:
                  if ( !string.IsNullOrEmpty( tbABSOpt10.Text.Trim() ) )
                     options.Add( tbABSOpt10.Text.Trim(), tbABSVal10.Text.Trim() );
                  break;
            }
         }
         return options;
      }
      // --------------------------------------------------------------------------------------
      protected Dictionary<string, string> get_Archive_encoded()
      {
         Dictionary<string, string> archive = new Dictionary<string, string>();
         archive.Add( "archive_type", "zip" );
         //
         if ( upload_archive.HasFile )
         {
         }
         return archive;
      }

   }
}